using System.Collections.Generic;
using UnityEngine;

public class AIManager : MonoBehaviour
{
	public Dictionary<string, TargetCollector> TargetList = new Dictionary<string, TargetCollector>();

	public int TargetTypeCount;

	public float UpdateInterval = 0.1f;

	private float timeTmp;

	public string PlayerTag = "Player";

	private void Start()
	{
		TargetList = new Dictionary<string, TargetCollector>();
	}

	public void Clear()
	{
		foreach (KeyValuePair<string, TargetCollector> target in TargetList)
		{
			if (target.Value != null)
			{
				target.Value.Clear();
			}
		}
		TargetList.Clear();
		TargetList = new Dictionary<string, TargetCollector>(0);
	}

	public TargetCollector FindTargetTag(string tag)
	{
		if (TargetList.ContainsKey(tag))
		{
			if (TargetList.TryGetValue(tag, out TargetCollector value))
			{
				value.IsActive = true;
				return value;
			}
			return null;
		}
		TargetList.Add(tag, new TargetCollector(tag));
		return null;
	}

	private void Update()
	{
		if (Time.time > timeTmp + UpdateInterval)
		{
			int num = 0;
			foreach (KeyValuePair<string, TargetCollector> target in TargetList)
			{
				if (target.Value != null && target.Value.IsActive)
				{
					target.Value.SetTarget(target.Key);
					target.Value.IsActive = false;
					num++;
				}
			}
			TargetTypeCount = num;
			timeTmp = Time.time;
		}
	}

	public bool IsPlayerAround(GameObject currentObject, float distance)
	{
		TargetCollector targetCollector = FindTargetTag(PlayerTag);
		if (targetCollector != null && targetCollector.Targets.Length > 0)
		{
			for (int i = 0; i < targetCollector.Targets.Length; i++)
			{
				if (targetCollector.Targets[i] != null && currentObject.transform.root != targetCollector.Targets[i].transform && Vector3.Distance(currentObject.transform.position, targetCollector.Targets[i].transform.position) <= distance)
				{
					return true;
				}
			}
		}
		return false;
	}
}
